import { Request, Response } from 'express';
import { AdminService } from '../services/admin.service';
import { AppError } from '../middlewares/errorHandler';

export class AdminController {
  private adminService: AdminService;

  constructor() {
    this.adminService = AdminService.getInstance();
  }

  // 设置超级管理员
  setAdmin = async (req: Request, res: Response) => {
    const { userId } = req.body;

    if (!userId) {
      throw new AppError('用户ID不能为空', 400);
    }

    // 检查当前用户是否有权限设置管理员
    const currentUserId = req.user?.id;
    if (!currentUserId) {
      throw new AppError('未授权', 401);
    }

    const isAdmin = await this.adminService.isUserAdmin(currentUserId);
    if (!isAdmin) {
      throw new AppError('没有权限执行此操作', 403);
    }

    await this.adminService.setUserAsAdmin(userId);
    res.json({
      success: true,
      message: '设置管理员成功'
    });
  };

  // 获取用户权限
  getUserPermissions = async (req: Request, res: Response) => {
    const { userId } = req.params;

    if (!userId) {
      throw new AppError('用户ID不能为空', 400);
    }

    const permissions = await this.adminService.getUserPermissions(userId);
    res.json({
      success: true,
      data: permissions
    });
  };

  // 获取用户角色
  getUserRoles = async (req: Request, res: Response) => {
    const { userId } = req.params;

    if (!userId) {
      throw new AppError('用户ID不能为空', 400);
    }

    const roles = await this.adminService.getUserRoles(userId);
    res.json({
      success: true,
      data: roles
    });
  };

  // 分配角色
  assignRole = async (req: Request, res: Response) => {
    const { userId, roleId } = req.body;

    if (!userId || !roleId) {
      throw new AppError('用户ID和角色ID不能为空', 400);
    }

    // 检查权限
    const currentUserId = req.user?.id;
    if (!currentUserId) {
      throw new AppError('未授权', 401);
    }

    const isAdmin = await this.adminService.isUserAdmin(currentUserId);
    if (!isAdmin) {
      throw new AppError('没有权限执行此操作', 403);
    }

    await this.adminService.assignRoleToUser(userId, roleId);
    res.json({
      success: true,
      message: '分配角色成功'
    });
  };

  // 移除角色
  removeRole = async (req: Request, res: Response) => {
    const { userId, roleId } = req.body;

    if (!userId || !roleId) {
      throw new AppError('用户ID和角色ID不能为空', 400);
    }

    // 检查权限
    const currentUserId = req.user?.id;
    if (!currentUserId) {
      throw new AppError('未授权', 401);
    }

    const isAdmin = await this.adminService.isUserAdmin(currentUserId);
    if (!isAdmin) {
      throw new AppError('没有权限执行此操作', 403);
    }

    await this.adminService.removeRoleFromUser(userId, roleId);
    res.json({
      success: true,
      message: '移除角色成功'
    });
  };

  // 创建角色
  createRole = async (req: Request, res: Response) => {
    const { name, description } = req.body;

    if (!name) {
      throw new AppError('角色名称不能为空', 400);
    }

    // 检查权限
    const currentUserId = req.user?.id;
    if (!currentUserId) {
      throw new AppError('未授权', 401);
    }

    const isAdmin = await this.adminService.isUserAdmin(currentUserId);
    if (!isAdmin) {
      throw new AppError('没有权限执行此操作', 403);
    }

    const roleId = await this.adminService.createRole(name, description);
    res.json({
      success: true,
      data: { roleId },
      message: '创建角色成功'
    });
  };
} 